package com.baby.wheelview;

/**
 * 数字滚轮适配器
 * 
 */
public class NumberWheelAdapter extends WheelAdapter {
	private int startNumber;
	private int endNumber;
	private int interval;
	private String label;
	// 格式
	private String format;

	/**
	 * 
	 * @param startNumber
	 *            开始的数字
	 * @param endNumber
	 *            结束的数字
	 * @param interval
	 *            间隔
	 * @param label
	 *            标记
	 * @param format
	 *            格式
	 */
	public NumberWheelAdapter(int startNumber, int endNumber, int interval, String label, String format) {
		this.startNumber = startNumber;
		this.endNumber = endNumber;
		this.interval = interval;
		this.label = label;
		this.format = format;
	}

	/***
	 * 
	 * @param startNumber
	 * @param endNumber
	 * @param interval
	 * @param format
	 */
	public NumberWheelAdapter(int startNumber, int endNumber, int interval, String format) {
		this(startNumber, endNumber, interval, null, format);
	}

	@Override
	public int getCount() {
		return (endNumber - startNumber) / interval;
	}

	@Override
	public String getItem(int index) {
		int number = startNumber + index * interval;
		String data = format != null ? String.format(format, number) : Integer.toString(number);
		if (TextUtil.isEmpty(label))
			return data;

		return data + label;
	}

	/**
	 * 设置起始数值
	 * 
	 * @param startItem
	 */
	public void setStartItem(int startItem) {
		this.startNumber = startItem * this.interval;
		this.notifyChanged();
	}

	@Override
	public int getValue(int index) {
		return index * this.interval + this.startNumber;
	}

	/**
	 * 获取当前起始数字
	 * 
	 * @return
	 */
	@Override
	public int getStartValue() {
		return this.startNumber;
	}

	/**
	 * 获取当前结束数字
	 * 
	 * @return
	 */
	@Override
	public int getEndValue() {
		return this.endNumber;
	}

	/**
	 * 获取数字间隔
	 * 
	 * @return
	 */
	@Override
	public int getInterval() {
		return this.interval;
	}

	@Override
	public int getValueIndex(int value) {
		return (value - this.startNumber) / this.interval;
	}

	@Override
	public void setStartValue(int value) {
		this.startNumber = value;
	}
}
